package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.DTO.PetDTO;
import com.example.demo.entity.PetAndUser;
import com.example.demo.entity.VO.PetAndUserVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IPetAndUserService;
import com.example.demo.service.IPetService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 宠物与主人中间表 前端控制器
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Api(tags = "宠物与主人接口")
@RestController
@RequestMapping("/pet-and-user")
public class PetAndUserController {

    @Resource
    private IPetAndUserService petAndUserService;

    @Resource
    private IUserService userService;

    @Resource
    private IPetService petService;

    /**
     * 通过userId查询该用户的所有宠物
     * @return
     */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "通过userId查询该用户的所有宠物")
    @PostMapping
    public List<PetDTO> findAllPetByUserId(int userId) {

        return petAndUserService.findAllPetsByUserId(userId);

    }


    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Result<String> add(@RequestBody PetAndUserVO petAndUserVO) {
        PetAndUser petAndUser = new PetAndUser();
        BeanUtil.copyProperties(petAndUserVO,petAndUser);
        //判断user是否存在
        if (userService.findNicknameByUserId(petAndUser.getUserId()) == null) {
            return Result.error().msg("用户不存在");
        }
        //判断宠物是否存在
        if (petService.findPetById(petAndUser.getPetId()) == null) {
            return Result.error().msg("宠物不存在");
        }
        //查询是否已经存在
        if (petAndUserService.selectByPetIdAndUserId(petAndUser).size() > 0) {
            return Result.error().msg("该宠物已经添加过了");
        }
        petAndUserService.insert(petAndUser);
        return Result.success().msg("添加成功");
    }

}